package com.javasoft.controller.ZSW;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasoft.entity.LCB.UserInfoEntity;
import com.javasoft.entity.XGX.Question;
import com.javasoft.entity.YZX.DataDictionaryType;
import com.javasoft.entity.ZSW.DataDictionaryEntity;
import com.javasoft.service.LCB.UserInfoService;
import com.javasoft.service.XGX.impl.QuestionImpl;
import com.javasoft.service.YZX._interface.DataDictionaryTypeService;
import com.javasoft.service.ZSW.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pagequery")
public class PaginationQueryController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 对用户信息表进行分页导出
     * @param page，分几页
     * @param line，分几行
     * @return
     */
    @GetMapping("/userpagequery")
    public PageImpl<UserInfoEntity> userpage(
            @RequestParam(defaultValue = "") String name,
            @RequestParam(defaultValue = "") Integer page, //current
            @RequestParam(defaultValue = "") Integer line //size
    ) {
        List<UserInfoEntity> allEntities = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
            Page<UserInfoEntity> currentPage = userInfoService.page(i, line, name);
            allEntities.addAll(currentPage.getRecords());
        }
        return new PageImpl<>(allEntities, PageRequest.of(1, allEntities.size()), allEntities.size());
    }

    /**
     * 对数据字典进行分页导出
     * @param page，分几页
     * @param line，分几行
     * @return
     */
    @Autowired
    private DataDictionaryService dataDictionaryService;

    @GetMapping("/datadicpagequery")
    public PageImpl<DataDictionaryEntity> datapage(
            @RequestParam(defaultValue = "") String type,//查找类型
            @RequestParam(defaultValue = "") Integer page,//页
            @RequestParam(defaultValue = "") Integer line//行
    ){
        List<DataDictionaryEntity> allEntities = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
            Page<DataDictionaryEntity> currentPage = dataDictionaryService.page(i, line, type);
            allEntities.addAll(currentPage.getRecords());
        }
        return new PageImpl<>(allEntities, PageRequest.of(1, allEntities.size()), allEntities.size());
    }

    /**
     * 对试卷表进行分页导出
     * @param page，分几页
     * @param line，分几行
     * @return
     */
    @Autowired
    private QuestionImpl question_impl;

    @GetMapping("/quetablepagequery")
    public Page<Question> quepage(
            @RequestParam(defaultValue = "1",value = "page") Integer page,
            @RequestParam(defaultValue = "15",value = "line") Integer line
    ) {
        return question_impl.getAllData(page, line);
    }

    /**
     * 对数据字典类型表进行分页导出
     * @param page，分几页
     * @param line，分几行
     * @return
     */
    @Autowired
    private DataDictionaryTypeService dataDictionaryTypeService;
    @GetMapping("/datedictypepagequery")
    public PageImpl<DataDictionaryType> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "line", defaultValue = "10") int line,
            @RequestParam(required = false) String fixedType
    ) {
        List<DataDictionaryType> allEntities = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
            Page<DataDictionaryType> currentPage = (Page<DataDictionaryType>) dataDictionaryTypeService.pageQuery(i, line, fixedType);
            allEntities.addAll(currentPage.getRecords());
        }
        return new PageImpl<>(allEntities, PageRequest.of(1, allEntities.size()), allEntities.size());
    }
}
